﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using LUStationery.Model;
using LUStationery.DAO;
using System.Collections;

namespace LUStationery.Controller
{
    public class PoControl
    {
        public string makePo(string supplierName, string deliverTo, string attn, DateTime expectedDate, string orderBy, ArrayList orderIDs)
        {
            SupplierControl scont = new SupplierControl();
            Supplier s = scont.getSupplierBySupplierName(supplierName);
            Po po = new Po(s, deliverTo, attn, expectedDate, orderBy);
            PoDAO pDAO = new PoDAO();

            //return the updated po with the purchase_order_id which is assigned by database system
            IDataReader idr = pDAO.addPo(po.SupplierCode, po.SupplierName, po.TotalPrice, po.Status, po.ExpectedDate, po.OrderDate, po.OrderBy, po.DeliverTo, po.Attn);
            po = convertIdrToPo(idr);
            double totalPrice = 0;
            OrdersControl ocont = new OrdersControl();
            foreach (int orderID in orderIDs)
            {
                Order o = ocont.getOrderByOrderID(orderID);
                o.PurchaseOrderID = po.PurchaseOrderID;
                ocont.addToPo(o);
                totalPrice += o.OrderPrice;


                OrdersControl ord = new OrdersControl();
                ord.updateOrder(po.PurchaseOrderID, "processing");
            }
            updateTotalPrice(po.PurchaseOrderID, totalPrice);
            return po.PurchaseOrderID.ToString();
        }

        public void updateTotalPrice(int purchaseOrderID, double totalPrice)
        {
            PoDAO pDAO = new PoDAO();
            pDAO.updateTotalPrice(purchaseOrderID, totalPrice);
        }

        public PoList getPoIDs() 
        {
            PoDAO poDAO = new PoDAO();
            IDataReader idr = poDAO.getPoIDs();
            return PoControl.convertIdrToPoList(idr);
        }

        public PoList getPObyPoID(string poID)
        {
            PoDAO poDAO = new PoDAO();
            IDataReader idr = poDAO.getPObyPoID(poID);
            return PoControl.convertIdrToPoList(idr);
        }

        protected static Po convertIdrToPo(IDataReader idr)
        {
            Po po = new Po();
            while (idr.Read())
            {
                po.SupplierCode = Convert.ToString(idr["supplier_code"]);
                po.SupplierName = Convert.ToString(idr["supplier_name"]);
                po.TotalPrice = Convert.ToDouble(idr["total_price"]);
                //po.Status = Convert.ToString(idr["purchase_order_status"]);
                //po.ExpectedDate = Convert.ToDateTime(idr["excepted_date"]);
                po.OrderDate = Convert.ToDateTime(idr["order_date"]);
                po.OrderBy = Convert.ToString(idr["order_by"]);


                string t1 = Convert.ToString(idr["purchase_order_id"]);
                if (t1 == "")
                {
                    po.PurchaseOrderID = 0;
                }
                else
                {
                    po.PurchaseOrderID = Convert.ToInt32(idr["purchase_order_id"]);
                }

                string t2 = Convert.ToString(idr["approve_by"]);
                if (t2 == "")
                {
                    po.ApproveBy = "";
                }
                else
                {
                    po.ApproveBy = Convert.ToString(idr["approve_by"]);
                }

            }
            return po;
        }

        protected static PoList convertIdrToPoList(IDataReader idr)
        {
            PoList pList = new PoList();
            while (idr.Read())
            {
                Po po = new Po();
                po.SupplierCode = Convert.ToString(idr["supplier_code"]);
                po.SupplierName = Convert.ToString(idr["supplier_name"]);
                po.TotalPrice = Convert.ToDouble(idr["total_price"]);
                //po.Status = Convert.ToString(idr["purchase_order_status"]);
                //po.ExpectedDate = Convert.ToDateTime(idr["excepted_date"]);
                po.OrderDate = Convert.ToDateTime(idr["order_date"]);
                po.OrderBy = Convert.ToString(idr["order_by"]);


                string t1 = Convert.ToString(idr["purchase_order_id"]);
                if (t1 == "")
                {
                    po.PurchaseOrderID = 0;
                }
                else
                {
                    po.PurchaseOrderID = Convert.ToInt32(idr["purchase_order_id"]);
                }

                string t2 = Convert.ToString(idr["approve_by"]);
                if (t2 == "")
                {
                    po.ApproveBy = "";
                }
                else
                {
                    po.ApproveBy = Convert.ToString(idr["approve_by"]);
                }
                pList.Add(po);
            }
            return pList;
        }
    }
}
